const api = require('../../network/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageList: {},
        chapterList: [],
        chapter_id: '',
        scrollTop: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option) {
        const {
            chapter_id,
            name,
            comic_id
        } = option
        console.log(chapter_id, 'chapter_id', comic_id, name);
        this.init(chapter_id)
        this.getChpater(comic_id, chapter_id)
    },
    async init(chapter_id) {
        const res = await api.getChapterPage({
            chapter_id
        })
        if (res.code == 200) {
            this.setData({
                pageList: res.data,
                chapter_id
            })
        }
    },
    // 获取章节列表
    async getChpater(comic_id, chapter_id) {
        const res = await api.cartoonDetail({ id: comic_id })
        if (res.code == 200) {
            console.log(res, '详情');
            const { read, price, charge } = res.data
            const user = wx.getStorageSync('userInfo')
            const param = {
                comic_id,
                chapter_id,
                uid: user.id,
                read,
                price,
                is_vip: charge
            }
            const log = await api.addRead(param)
            console.log(log, '添加情况');
        }
        const res1 = await api.getChapter({
            comic_id,
            isAll: true
        })
        if (res1.code == 200) {
            const item = res1.data.data.find(item => item.chapter_id == chapter_id);
            wx.setNavigationBarTitle({
                title: item.title,
            })
            this.setData({
                chapterList: res1.data.data,
            })
        }
    },
    // 切换章节
    tabChapter(e) {
        const {
            id,
            type
        } = e.currentTarget.dataset
        console.log(id, type);
        const length = this.data.chapterList.length
        const index = this.data.chapterList.findIndex(item => item.chapter_id == id);
        let num = 0
        if (type == 1 && index != -1) {
            num = index - 1
            console.log(num, 'num');
            if (num < 0) {
                console.log('到底了');
                wx.showToast({
                    title: '当前是第一话',
                })
                return
            }
        } else {
            num = index + 1

            if (num >= length) {
                // console.log('已经是最新章节');
                wx.showToast({
                    title: '已经是最新章节',
                })
                return
            }
        }
        const item = this.data.chapterList[num]
        wx.setNavigationBarTitle({
            title: item.title,
        })
        console.log(num, item);
        this.init(item.chapter_id)
        this.getChpater(item.comic_id, item.chapter_id)
        this.scrollToTop()
    },
    lower() {

    },
    scroll(e) {
        // console.log(e,'scroll')
    },
    scrollToTop() {
        this.setData({
            scrollTop: 0
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})